//
//  TTDetailFilterCustomHeaderView.m
//  TaoTuo
//
//  Created by admin on 2019/12/11.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDetailFilterCustomHeaderView.h"
#import "TTFiltrateView.h"

#define customRowH 30
#define titleW 40

@interface TTDetailFilterCustomHeaderView ()

@property (nonatomic, strong) UILabel *xkLabel;
@property (nonatomic, strong) TTFiltrateView *xkFilterView;

@property (nonatomic, strong) UILabel *scLabel;
@property (nonatomic, strong) TTFiltrateView *scFilterView;

@property (nonatomic, strong) UILabel *njLabel;
@property (nonatomic, strong) TTFiltrateView *njFilterView;

@property (nonatomic, strong) UILabel *csLabel;
@property (nonatomic, strong) TTFiltrateView *csFilterView;

@property (nonatomic, strong) UILabel *zgLabel;
@property (nonatomic, strong) TTFiltrateView *zgFilterView;

@property (nonatomic, strong) TTCustomizedModel *customModel;

@property (nonatomic, copy) SelectFinishedBlock selectBlock;

///参数字典
@property (nonatomic, strong) NSMutableDictionary *paramsDic;

@end

@implementation TTDetailFilterCustomHeaderView

+ (instancetype)customHeaderViewWithCustomizedModel:(TTCustomizedModel *)customModel selectFinished:(nonnull SelectFinishedBlock)selectFinishedBlock
{
    CGFloat h = 3 * customRowH;
    if (customModel.HandSizeList.count > 0) {
        h = 4 * customRowH;
        if (customModel.BraceletSizeList.count > 0) {
            h = 5 * customRowH;
        }
    } else {
        if (customModel.BraceletSizeList.count > 0) {
            h = 4 * customRowH;
        }
    }
    
    return [[self alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, h) customizedModel:customModel selectFinished:selectFinishedBlock];
}

- (instancetype)initWithFrame:(CGRect)frame customizedModel:(TTCustomizedModel *)customModel selectFinished:(nonnull SelectFinishedBlock)selectFinishedBlock
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.customModel = customModel;
        self.selectBlock = selectFinishedBlock;
        self.paramsDic = [NSMutableDictionary dictionary];
        
        [self setupSubviews];
        
    }
    return self;
}

- (void)setupSubviews
{
    _xkLabel = [UILabel labelWithTitle:@"镶口:" color:UIColorHex(333333) fontSize:12];
    _xkLabel.frame = CGRectMake(0, 0, titleW, customRowH);
    _xkLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_xkLabel];
    
    NSMutableArray *xkArray = [NSMutableArray array];
    for (NSString *str in self.customModel.InsertSizeList) {
        [xkArray addObject:[NSString stringWithFormat:@"%@分", str]];
    }
    @weakify(self)
    _xkFilterView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(titleW, _xkLabel.top, self.width - titleW, customRowH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        if (selectedArray.count > 0) {
            NSString *str = selectedArray.firstObject;
            str = [str substringToIndex:str.length - 1];
            
            [self.paramsDic setObject:str forKey:@"param.insertSize"];
        } else {
            [self.paramsDic removeObjectForKey:@"param.insertSize"];
        }
        
        self.selectBlock(self.paramsDic);
        
    } leftRightSpace:5 cornerRadius:3];
    _xkFilterView.dataArray = xkArray;
    [self addSubview:_xkFilterView];
    
    _scLabel = [UILabel labelWithTitle:@"手寸:" color:UIColorHex(333333) fontSize:12];
    _scLabel.frame = CGRectMake(0, _xkLabel.bottom, titleW, customRowH);
    _scLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_scLabel];
    
    NSMutableArray *scArray = [NSMutableArray array];
    for (NSString *str in self.customModel.HandSizeList) {
        [scArray addObject:[NSString stringWithFormat:@"%@#", str]];
    }
    _scFilterView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(titleW, _scLabel.top, self.width - titleW, customRowH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        if (selectedArray.count > 0) {
            NSString *str = selectedArray.firstObject;
            str = [str substringToIndex:str.length - 1];
            [self.paramsDic setObject:str forKey:@"param.handSize"];
        } else {
            [self.paramsDic removeObjectForKey:@"param.handSize"];
        }
        
        self.selectBlock(self.paramsDic);
        
    } leftRightSpace:5 cornerRadius:3];
    _scFilterView.dataArray = scArray;
    [self addSubview:_scFilterView];
    
    _scLabel.hidden = self.customModel.HandSizeList.count == 0;
    _scFilterView.hidden = _scLabel.hidden;
    
    CGFloat njTop = _scLabel.hidden ? _scLabel.top : _scLabel.bottom;
    _njLabel = [UILabel labelWithTitle:@"内径:" color:UIColorHex(333333) fontSize:12];
    _njLabel.frame = CGRectMake(0, njTop, titleW, customRowH);
    _njLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_njLabel];
    
    _njFilterView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(titleW, _njLabel.top, self.width - titleW, customRowH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        
        if (selectedArray.count > 0) {
            [self.paramsDic setObject:selectedArray.firstObject forKey:@"param.braceletSize"];
        } else {
            [self.paramsDic removeObjectForKey:@"param.braceletSize"];
        }
        
        self.selectBlock(self.paramsDic);
        
    } leftRightSpace:5 cornerRadius:3];
    _njFilterView.dataArray = self.customModel.BraceletSizeList;
    [self addSubview:_njFilterView];
    
    _njLabel.hidden = self.customModel.BraceletSizeList.count == 0;
    _njFilterView.hidden = _njLabel.hidden;
    
    
    CGFloat csTop = _njLabel.hidden ? _njLabel.top : _njLabel.bottom;
    _csLabel = [UILabel labelWithTitle:@"成色:" color:UIColorHex(333333) fontSize:12];
    _csLabel.frame = CGRectMake(0, csTop, titleW, customRowH);
    _csLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_csLabel];
    
    _csFilterView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(titleW, _csLabel.top, self.width - titleW, customRowH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        if (selectedArray.count > 0) {
            [self.paramsDic setObject:selectedArray.firstObject forKey:@"param.goldColor"];
        } else {
            [self.paramsDic removeObjectForKey:@"param.goldColor"];
        }
        
        self.selectBlock(self.paramsDic);
        
    } leftRightSpace:5 cornerRadius:3];
    _csFilterView.dataArray = self.customModel.GoldColorList;
    [self addSubview:_csFilterView];
    
    _zgLabel = [UILabel labelWithTitle:@"做工:" color:UIColorHex(333333) fontSize:12];
    _zgLabel.frame = CGRectMake(0, _csLabel.bottom, titleW, customRowH);
    _zgLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_zgLabel];
    
    NSMutableArray *zgArray = [NSMutableArray array];
    for (TTFactoryGradeModel *model in self.customModel.FactoryGrade) {
        [zgArray addObject:model.ButtonName];
    }
    __block NSString *lastString = nil;
    _zgFilterView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(titleW, _zgLabel.top, self.width - titleW, customRowH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        NSInteger value = 0;
        for (TTFactoryGradeModel *model in self.customModel.FactoryGrade) {
            if ([model.ButtonName isEqualToString:selectedArray.firstObject]) {
                value = model.ButtonValue;
                break;
            }
        }
        if (selectedArray.count > 0) {
            [self.paramsDic setObject:@(value) forKey:@"param.goodsGrade"];
        } else {
            [self.paramsDic removeObjectForKey:@"param.goodsGrade"];
        }
    
        if (![lastString isEqualToString:selectedArray.firstObject]) {
            self.selectBlock(self.paramsDic);
        }
        
        lastString = selectedArray.firstObject;
        
    } leftRightSpace:5 cornerRadius:3];
    _zgFilterView.dataArray = zgArray;
    _zgFilterView.canCancelSelect = NO;
    [_zgFilterView setSelectedFirstItem];
    [self addSubview:_zgFilterView];
    
    
}


@end
